#include <stdio.h>
#include <omp.h>

// 模拟计算
float foo(int i) { return (float)(i * 2); }
float bar(float a, float b) { return a + b; }
float baz(float b) { return b * 2.0f; }

void work(int N, float *A, float *B, float *C) {
    int i;

    #pragma omp parallel for ordered(1)
    for (i = 1; i < N; i++) {
        A[i] = foo(i);  // 先独立计算 A[i]

        // 需要等待 B[i-1] 计算完成
        #pragma omp ordered doacross(sink: i-1)
        B[i] = bar(A[i], B[i - 1]);

        // 发出信号，表示 B[i] 已准备好
        #pragma omp ordered doacross(source: omp_cur_iteration)
        C[i] = baz(B[i]);
    }
}

int main() {
    int N = 10;
    float A[10] = {0}, B[10] = {0}, C[10] = {0};
    B[0] = 1.0;

    work(N, A, B, C);

    for (int i = 1; i < N; i++)
        printf("C[%d] = %.1f\\n", i, C[i]);

    return 0;
}
